#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/kvm.h>

#define KVM_FILE "/dev/kvm"

int main(int argc, char *argv[])
{
    int kvm_fd;
    int ret;

    kvm_fd = open(KVM_FILE, O_RDWR | O_NDELAY);
    ret = ioctl(kvm_fd, KVM_GET_API_VERSION, 0);
    printf("----KVM API version is --%d--\n", ret);

    /* 在KVM API版本12之前的版本，我们应该使用KVM_CAP_MAX_VCPUS来获得
       每个虚拟机最大的CPU数量 */
    ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS);
    printf("----KVM supports MAX_VCPUS per guest(VM) is --%d--\n", ret);

    ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_IOMMU);
    if (ret != 0) {
      printf("----KVM supports IOMMU (i.e. Intel VT-d or AMD IOMMU).----\n");
    } else {
      printf("----KVM do not support IOMMU (i.e. Intel VT-d or AMD IOMMU).----\n");
    }
    
    return 0;
}
